#include "chartform.h"
#include "ui_chartform.h"

#include <qpainter.h>
static int s_curve_id = 0;

ChartForm::ChartForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ChartForm)
{
    ui->setupUi(this);

    qreal unit = ui->coordinate->unit();
    int unitSpace = ui->coordinate->unitSpace();

    ui->ruler_Top->setOrientation(Qt::Horizontal);
    ui->ruler_Top->setSpace(40);
    ui->ruler_Top->setUnit(unit);
    ui->ruler_Top->setUnitSpace(unitSpace);
    ui->ruler_Top->setAlign(Qt::AlignHCenter | Qt::AlignBottom);
    ui->ruler_Bottom->setOrientation(Qt::Horizontal);
    ui->ruler_Bottom->setSpace(40);
    ui->ruler_Bottom->setUnit(unit);
    ui->ruler_Bottom->setUnitSpace(unitSpace);
    ui->ruler_Bottom->setAlign(Qt::AlignHCenter | Qt::AlignTop);
    ui->ruler_Left->setOrientation(Qt::Vertical);
    ui->ruler_Left->setSpace(20);
    ui->ruler_Left->setUnit(unit);
    ui->ruler_Left->setUnitSpace(unitSpace);
    ui->ruler_Left->setAlign(Qt::AlignCenter);
    ui->ruler_Right->setOrientation(Qt::Vertical);
    ui->ruler_Right->setSpace(20);
    ui->ruler_Right->setUnit(unit);
    ui->ruler_Right->setUnitSpace(unitSpace);
    ui->ruler_Right->setAlign(Qt::AlignVCenter | Qt::AlignLeft);
    connect(ui->coordinate, &Graph::startPositionChanged, ui->ruler_Top, &Ruler::changedStart);
    connect(ui->coordinate, &Graph::startPositionChanged, ui->ruler_Bottom, &Ruler::changedStart);
    connect(ui->coordinate, &Graph::startPositionChanged, ui->ruler_Left, &Ruler::changedStart);
    connect(ui->coordinate, &Graph::startPositionChanged, ui->ruler_Right, &Ruler::changedStart);
    connect(ui->coordinate, &Graph::zoomChanged, ui->ruler_Top, &Ruler::changedZoom);
    connect(ui->coordinate, &Graph::zoomChanged, ui->ruler_Bottom, &Ruler::changedZoom);
    connect(ui->coordinate, &Graph::zoomChanged, ui->ruler_Left, &Ruler::changedZoom);
    connect(ui->coordinate, &Graph::zoomChanged, ui->ruler_Right, &Ruler::changedZoom);

    connect(ui->coordinate, &Graph::selectPoint, this, &ChartForm::positionChanged);
    connect(ui->coordinate, &Graph::movePoint, this, &ChartForm::movePoint);
    connect(ui->coordinate, &Graph::hasSelectedPoint, this, &ChartForm::selectPointChanged);
    connect(this, &ChartForm::changedPosition, ui->coordinate, &Graph::changedPosition);

}

ChartForm::~ChartForm()
{
    delete ui;
}

QString ChartForm::addCurve(const QString& name)
{
    QString id = ui->coordinate->addCurve(name);

    QColor color = ui->coordinate->curveColor(id);
    QPixmap pixmap(":/icons/icons_curve.png");
    QPainter painter(&pixmap);
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.fillRect(pixmap.rect(), color);
    QIcon colorIcon = QIcon(pixmap);

    m_curveIds.emplace(id);
    ui->comboBoxEditCurve->addItem(colorIcon, name, id);
    if(ui->comboBoxEditCurve->currentIndex() == -1){
        ui->comboBoxEditCurve->setCurrentIndex(0);
    }
    return id;
}

void ChartForm::selectCurve(const QString& id)
{
    ui->coordinate->selectCurve(id);

    if(m_curveIds.count(id))
        return;
    QString name = ui->coordinate->curveName(id);
    QColor color = ui->coordinate->curveColor(id);
    QPixmap pixmap(":/icons/icons_curve.png");
    QPainter painter(&pixmap);
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.fillRect(pixmap.rect(), color);
    QIcon colorIcon = QIcon(pixmap);

    ui->comboBoxEditCurve->addItem(colorIcon, name, id);
    if(ui->comboBoxEditCurve->currentIndex() == -1){
        ui->comboBoxEditCurve->setCurrentIndex(0);
    }

}

void ChartForm::clearSelectCurve(const QString& id)
{
    if(id.isEmpty()){
        ui->comboBoxEditCurve->clear();
        m_curveIds.clear();

    }else if(m_curveIds.count(id)){
        m_curveIds.erase(id);
        int index = ui->comboBoxEditCurve->findData(id);
        ui->comboBoxEditCurve->removeItem(index);
    }

    ui->coordinate->clearSelect(id);
}

void ChartForm::deleteCurve(const QString& id)
{
    if(m_curveIds.count(id)){
        m_curveIds.erase(id);
        int index = ui->comboBoxEditCurve->findData(id);
        ui->comboBoxEditCurve->removeItem(index);
    }
    ui->coordinate->deleteCurve(id);
}

QColor ChartForm::getCurveColor(const QString &id) const
{
    return ui->coordinate->curveColor(id);
}


void ChartForm::positionChanged(QPointF position)
{
    ui->doubleSpinBoxX->setValue(position.x());
    ui->doubleSpinBoxY->setValue(position.y());
}

void ChartForm::selectPointChanged(bool hasSelect)
{
    ui->doubleSpinBoxX->setEnabled(hasSelect);
    ui->doubleSpinBoxY->setEnabled(hasSelect);
}

void ChartForm::movePoint(QPointF offset)
{
    QSignalBlocker blocker(ui->coordinate);
    ui->doubleSpinBoxX->setValue(ui->doubleSpinBoxX->value() + offset.x());
    ui->doubleSpinBoxY->setValue(ui->doubleSpinBoxY->value() + offset.y());
}


void ChartForm::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QRect rect = ui->frameChart->geometry();

    int hSpace = ui->ruler_Top->space();
    int vSpace = ui->ruler_Left->space();

    ui->ruler_Top->setGeometry(0, 0, rect.width(), vSpace);
    ui->ruler_Bottom->setGeometry(0, rect.height() - vSpace, rect.width(), vSpace);

    ui->ruler_Left->setGeometry(0, 0, hSpace, rect.height());
    ui->ruler_Right->setGeometry(rect.width() - hSpace, 0, hSpace, rect.height());

    ui->coordinate->setGeometry(hSpace, vSpace, rect.width() - hSpace * 2, rect.height() - vSpace * 2);


}


void ChartForm::on_doubleSpinBoxY_editingFinished()
{
    QPointF position(ui->doubleSpinBoxX->value(), ui->doubleSpinBoxY->value());
    emit changedPosition(position);
}

void ChartForm::on_doubleSpinBoxX_editingFinished()
{
    QPointF position(ui->doubleSpinBoxX->value(), ui->doubleSpinBoxY->value());
    emit changedPosition(position);
}


void ChartForm::on_comboBoxEditCurve_currentIndexChanged(int index)
{
    QString id = ui->comboBoxEditCurve->itemData(index).toString();
    ui->coordinate->setEditingCurve(id);
}

